/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.systemtray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;

import softstarter.gui.subareas.controlbuttons.ControlButtonsListener;


public class SystemTrayFunctionality {
    
    private TrayMouseListener trayMouseListener;
    private ControlButtonsListener buttonsListener;
    
    public SystemTrayFunctionality(TrayMouseListener trayMouseListener, ControlButtonsListener buttonsListener) {
        this.trayMouseListener = trayMouseListener;
        this.buttonsListener = buttonsListener;
        createSystemTrayFunctionality();
    }
    
    private void createSystemTrayFunctionality() {
        
        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage("./resources/tray.gif");
                                
            PopupMenu popup = new PopupMenu();
            MenuItem exitMenuItem = new MenuItem("Exit");
            exitMenuItem.addActionListener(buttonsListener);
            popup.add(exitMenuItem);

            TrayIcon trayIcon = new TrayIcon(image, "Firesoft", popup);
                    
            trayIcon.setImageAutoSize(true);
            trayIcon.addMouseListener(trayMouseListener);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println("Failed to add tray icon.");
            }

        } 
        
    }

}
